﻿/*======================================================================
== Copyright : BlueCurve (c)
== Licence   : Gnu/GPL v2.x
== Author    : Teddy Albina
== Email     : bluecurveteam@gmail.com
== Web site  : http://www.codeplex.com/BlueCurve
========================================================================*/
using System.IO;
using System.Linq;
using System.Net;
using System.Collections.Generic;
using System.Xml.Linq;

namespace BlueCurve.Common.Crawlers
{
    /// <summary>
    /// Class d'authentification du réseau
    /// </summary>
    public class Authentification
    {
        /// <summary>
        /// Renvoie un credential si possible
        /// </summary>
        /// <param name="data">Données XML pour construire l'instance de Networkcredendial</param>
        /// <returns>Objet d'authentification</returns>
        public static NetworkCredential MakeUriCredential(string data)
        {
            try
            {
                NetworkCredential credential = new NetworkCredential();

                XDocument doc = XDocument.Load(new StringReader(data));

                var cred = from x in doc.Descendants("credential")
                           select new
                           {
                               Computer = x.Element("computer").Value,
                               Password = x.Element("password").Value,
                               Id = x.Element("id").Value
                           };

                foreach (var x in cred)
                {
                    if (!string.IsNullOrEmpty(x.Id))
                        credential.UserName = x.Id;
                    else
                        return null;

                    if (!string.IsNullOrEmpty(x.Password))
                        credential.Password = x.Password;
                    if (!string.IsNullOrEmpty(x.Computer))
                        credential.Domain = x.Computer;
                }

                return credential;
            }
            catch
            {
                return null;
            }
        }


        /// <summary>
        /// Renvoie un dictionnaire contenant le nom d'utilisateur
        /// et le mot de passe à utiliser pour la connexion
        /// </summary>
        /// <param name="data">Données XML pour construire le dictionnaire</param>
        /// <returns>Objet d'authentification</returns>
        public static Dictionary<string, string> GetConnectionDatas(string data)
        {
            try
            {
                Dictionary<string, string> auth = new Dictionary<string, string>(2);
                XDocument doc = XDocument.Load(new StringReader(data));

                var cred = from x in doc.Descendants("credential")
                           select new
                           {
                               Password = x.Element("password").Value,
                               Id = x.Element("id").Value
                           };

                foreach (var x in cred)
                {
                    if (!string.IsNullOrEmpty(x.Id))
                        auth.Add("username", x.Id);
                    else
                        return null;

                    if (!string.IsNullOrEmpty(x.Password))
                        auth.Add("password", x.Password);
                }

                return auth;
            }
            catch
            {
                return null;
            }
        }
    }
}
